-- SPDX-License-Identifier: GPL-3.0-or-later
---@class StoryData: Object
local Box = class("Box")
local WU = require "packages/wdtx/WUltiity"
function Box:initialize(player,data,obj)
  self.player = player
  self.room = player.room
  self.boxdata = data
  self.wdtxobj = obj
  self:initBoxSkills()
end

function Box:initBoxSkills()
  local boxdata = self:getBoxData()
  self.boxskills = table.clone(boxdata.skills)
end

function Box:getBoxData()
  return self.boxdata[self.wdtxobj.storyid]
end

function Box:removeSameSkills(skills)
  WU:removeSameItems(skills,self.boxskills)
end

function Box:getBoxItems(num,boxtype)   --获得宝箱开出的东西（卡牌，技能....）
  local boxdata = self:getBoxData()
  local weight_data = WU:getItemsByWeight(num,boxdata.weight)
  --检索技能字符串，去掉并且再配置里检索去除数量的技能数列表
  local skills = {}
  local skillnum = #table.filter(weight_data,function(ty) return ty == "skills" end)
  if (not boxtype or boxtype == 1) and skillnum > 0 then
    local skillinx = 0
    while skillinx < skillnum do
      table.removeOne(weight_data,"skills")
      skillinx = skillinx + 1
    end
    skills = table.random(self.boxskills,skillnum)
  else
    if skillnum > 0 then
      for i = 1,skillnum do
        table.removeOne(weight_data,"skills")
      end
    end
  end
  --检索随机到卡牌类型随机的卡牌
  local cards = {}
  if (not boxtype or boxtype == 2) and #weight_data > 0 then
    local data = {}
    local cardtypes = {}
    for _,t in ipairs(weight_data) do
      local name = WU:getItemByRate(boxdata[t],data)
      if not data[name] then
        data[name] = 1
        cardtypes[name] = t
      else
        data[name] = data[name] + 1
      end
    end

    cards = self.wdtxobj:findCards(data,self.room.void)
    for _,cid in ipairs(cards) do
      local card = Fk:getCardById(cid)
      local tp = cardtypes[card.name]
      local value = boxdata[tp][card.name].uplv
      if type(value) == "number" then
        self.wdtxobj:setCardMark(card,value)
      end
      if type(value) == "table" then
        self.wdtxobj:setCardMark(card,math.random(value[1],value[2]))
      end
    end
  end
  return {skills,cards}
end
--获得宝箱物品列表
function Box:openBox(boxtype)
  local getitemsdata = self:getBoxItems(6,boxtype)
  if getitemsdata[1] == 0 and getitemsdata[2] == 0 then
    room:doBroadcastNotify("ShowToast","宝箱刷不出什么东西了...（找开发者反馈）")
    return
  end
  -- if #getitemsdata[2] > 0 then
  --   for _, cid in ipairs(getitemsdata[2]) do
  --     local card = Fk:getCardById(cid)
  --     p(card.name)
  --     p(card.mark)
  --   end
  -- end
  -- p(getitemsdata)
  -- dbg()
  local skillstr,cardstr = "",""
  local selskillsnum,selcardsnum = 0,0
  if #getitemsdata[1] > 0 then
    skillstr = "从"..#getitemsdata[1].."个技能里选择1个技能。"
    selskillsnum = 1
  end
  if #getitemsdata[2] > 0 then
    cardstr = "从"..#getitemsdata[2].."张卡牌选择1张卡牌加入卡组"
    selcardsnum = 1
  end
  
  local reply = self.room:askToCustomDialog(self.player,{
    skill_name = "#wdtx_rule",
    qml_path = "packages/wdtx/qml/ChoiceAndCardsBox.qml",
    extra_data = {"#wdtx_rule",skillstr+cardstr,getitemsdata[1],getitemsdata[2],{selskillsnum,selcardsnum},{"OK"}},
  })
  if reply ~= "" then
    reply = reply
    -- p(reply)
    -- dbg()
    if reply.cards and #reply.cards > 0 then
      self.wdtxobj:inputPipleByGet(reply.cards,"self_draws")
      --清除无牌区域卡牌升级标记
      for _, cid in ipairs(getitemsdata[2]) do
        local card = Fk:getCardById(cid,true)
        if not table.contains(reply.cards,cid) and next(card.mark) then
          for m,v in pairs(card.mark) do
            if v ~= 0 then
              -- p(card.name)
              -- p(card.mark)
              -- dbg()
              self.room:setCardMark(card,m,0)
            end
          end
        end
      end
      -- local mk = {}
      -- for _,cid in ipairs(self.room.void) do
      --   local card = Fk:getCardById(cid,true)
      --   if next(card.mark) then
      --     table.insert(mk,{name=card.name,mark = card.mark})
      --   end
      -- end
      -- p(mk)
      -- dbg()
    end
    if reply.choices and #reply.choices > 0 then
      self.wdtxobj:removeSameSkills(reply.choices)
      local skillstr = table.concat(reply.choices,"|")
      self.room:handleAddLoseSkills(self.player, skillstr, nil, false)
    end
  end
end

return Box
